package org.csu.webjpetstoreback.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.webjpetstoreback.config.CommonResponse;


import org.csu.webjpetstoreback.mapper.InventoryMapper;
import org.csu.webjpetstoreback.mapper.ItemsMapper;
import org.csu.webjpetstoreback.model.Inventory;
import org.csu.webjpetstoreback.model.Item;
import org.csu.webjpetstoreback.model.Orders;
import org.csu.webjpetstoreback.model.Product;
import org.csu.webjpetstoreback.service.ItemService;
import org.csu.webjpetstoreback.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;


@Service
public class ItemServiceImpl extends ServiceImpl<ItemsMapper,Item> implements ItemService {

    @Autowired
    InventoryMapper inventoryMapper;
    @Autowired
    ItemsMapper itemsMapper;


    @Override
    public CommonResponse<Inventory> PutInventory(Inventory inventory) {
        UpdateWrapper<Inventory> UpdateWrapper = new UpdateWrapper<>();
        int result = inventoryMapper.update(inventory,UpdateWrapper);
        if (result ==1){
            return CommonResponse.BackForSuccess("inventory修改成功");
        }else{
            return CommonResponse.BackForError("inbentory修改失败");
        }
    }

    @Override
    public CommonResponse<Inventory> getInventoryByItemId(String itemId) {

        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemid",itemId);
        Inventory inventory = inventoryMapper.selectOne(queryWrapper);
        if (inventory != null){
            return CommonResponse.BackForSuccess(inventory);
        }else {
            return CommonResponse.BackForError("该inventory不存在");
        }
    }

    @Override
    public CommonResponse<List<Item>> GetItemListByProductId(String productId) {

        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productId);
        List<Item> items =itemsMapper.selectList(queryWrapper);
        if (items.size() > 0){
            return CommonResponse.BackForSuccess(items);
        }else{
            return CommonResponse.BackForError("当前不存在任何item ");
        }
    }

    @Override
    public CommonResponse<Item> GetItemByItemId(String itemId) {

        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemid",itemId);
        Item item =itemsMapper.selectOne(queryWrapper);
        if (item!=null){
            return CommonResponse.BackForSuccess(item);
        }else{
            return CommonResponse.BackForError("找不到该Id的item");
        }
    }

    @Override
    public void updateInventoryQuantity(Map<String, String> param){
        UpdateWrapper<Inventory> UpdateWrapper = new UpdateWrapper<>();
        String itemId = param.get("itemId");//这里存有疑问
        String increment1 =param.get("increment");
        Integer increment = Integer.valueOf(increment1);
        System.out.println("in ItemService itemId and increment has been updated");
        Inventory inventory = getInventoryByItemId(itemId).getData();
        UpdateWrapper.eq("itemid",itemId);
        UpdateWrapper.set("qty",inventory.getQty()-increment);
        int result = inventoryMapper.update(inventory,UpdateWrapper);
        System.out.println(result);
        if (result ==1){
            System.out.println("inventory修改成功");
        }else{
            System.out.println("inventory修改失败");
        }
    }
}



